import { Byte } from '../../api/io/Byte';
import { ITimer } from '../../api/timer/ITimer';
import { IAsync } from '../../api/event/IAsync';



/**观看广告属性枚举*/
export const enum E_WatchAd {
	/**开始看的开始时间*/
	WATCH_START_TIME = 0,
	/**上次看的结束时间*/
	LAST_WATCH_END_TIME = 1,
	/**枚举数量*/
	EnumCount = 2
}


/**观看广告*/
export class WatchAd {
	/**事件ID*/
	public static readonly EVENT_ID = 'STRUCT_DATA_WATCH_AD_EVENT_ID';
	/**整体更新事件ID（部分只需要监听整体变化）*/
	public static readonly TOTAL_EVENT_ID = 'STRUCT_DATA_WATCH_AD_TOTAL_EVENT_ID';
	/**缓存池*/
	private static POOL: WatchAd[] = [];

	/**创建入口*/
	public static create(json?: string): WatchAd {
		const c = this.POOL.pop() || new WatchAd();
		c._destroyed = false;
		c.resetProperty();
		if (json) {
			c.parse(json);
		}
		return c;
	}


	/**已释放*/
	protected _destroyed = false;
	/**开始看的开始时间 (long)*/
	protected _watchStartTime: number;
	/**上次看的结束时间 (long)*/
	protected _lastWatchEndTime: number;


	/**构建函数-私有，防止外部创建*/
	private constructor() {
	}


	/**释放*/
	public destroy(): void {
		if (this._destroyed) {
			return;
		}
		this._destroyed = true;
		this.resetProperty(true);
		if (WatchAd.POOL.indexOf(this) === -1) {
			WatchAd.POOL.push(this);
		}
	}

	/**开始看的开始时间*/
	public get watchStartTime() {
		return this._watchStartTime;
	}


	/**开始看的开始时间*/
	public set watchStartTime(v: number) {
		this._watchStartTime = v;
	}


	/**上次看的结束时间*/
	public get lastWatchEndTime() {
		return this._lastWatchEndTime;
	}


	/**上次看的结束时间*/
	public set lastWatchEndTime(v: number) {
		this._lastWatchEndTime = v;
	}



	/**通过枚举获取值*/
	public getV(k: E_WatchAd): any {
		switch (k) {
			case E_WatchAd.WATCH_START_TIME: return this._watchStartTime;
			case E_WatchAd.LAST_WATCH_END_TIME: return this._lastWatchEndTime;
			default: return null;
		}
	}


	/**通过枚举设置值*/
	public setV(k: E_WatchAd, v: any): void {
		switch (k) {
			case E_WatchAd.WATCH_START_TIME: this._watchStartTime = v; break;
			case E_WatchAd.LAST_WATCH_END_TIME: this._lastWatchEndTime = v; break;
			default: break;
		}
	}


	/**初始化*/
	public resetProperty(fromDestroy = false): void {
		//开始看的开始时间
		this._watchStartTime = 0;
		//上次看的结束时间
		this._lastWatchEndTime = 0;
	}


	/**序列化*/
	public stringify(toString: boolean): string | any {
		const json: any = {};
		if (this._watchStartTime) {
			json.watchStartTime = this._watchStartTime;
		}
		if (this._lastWatchEndTime) {
			json.lastWatchEndTime = this._lastWatchEndTime;
		}

		return toString ? JSON.stringify(json) : json;
	}


	/**反序列化*/
	public parse(jsonv: string | any): this {
		this.resetProperty();
		if (!jsonv) {
			return this;
		}
		const json = typeof (jsonv) === 'string' ? JSON.parse(jsonv) : jsonv;
		if (json.watchStartTime) {
			this._watchStartTime = json.watchStartTime;
		}
		if (json.lastWatchEndTime) {
			this._lastWatchEndTime = json.lastWatchEndTime;
		}
		return this;
	}


	/**序列化-二进制*/
	public toBytes(byte: Byte): void {
		if (!byte) {
			return;
		}
		byte.writeLongNumber(this._watchStartTime);
		byte.writeLongNumber(this._lastWatchEndTime);
	}


	/**反序列化-二进制*/
	public fromBytes(byte: Byte): this {
		this.resetProperty();
		if (!byte) {
			return;
		}
		this._watchStartTime = byte.readLongNumber();
		this._lastWatchEndTime = byte.readLongNumber();
		return this;
	}


	/**克隆*/
	public clone(): WatchAd {
		const byte = Byte.createFromPool();
		this.toBytes(byte);
		byte.pos = 0;
		const clonev = WatchAd.create().fromBytes(byte);
		Byte.recoverToPool(byte);
		return clonev;
	}


	/**处理更新*/
	public processUpdate(jsonv: string): void {
		if (!jsonv) {
			return;
		}

		const eventID = WatchAd.EVENT_ID;
		const us = JSON.parse(jsonv);

		for (let i = 0, len = us.length; i < len;) {
			const idx = us[i];
			//开始看的开始时间
			if (idx === E_WatchAd.WATCH_START_TIME) {
				const oldv = this._watchStartTime;
				this._watchStartTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._watchStartTime, oldv);
				i += 2;
				continue;
			}
			//上次看的结束时间
			if (idx === E_WatchAd.LAST_WATCH_END_TIME) {
				const oldv = this._lastWatchEndTime;
				this._lastWatchEndTime = us[i + 1];
				IAsync.dispatch(eventID, idx, this._lastWatchEndTime, oldv);
				i += 2;
				continue;
			}

			console.error('[Struct Data] can not find enum in WatchAd with idx = ' + idx + ', version maybe wrong.')
		}

		//全更事件
		ITimer.callLater(this, this.dispatchTotalUpdate);
	}


	/**比对不一致结果*/
	public getCompareInfo(other: WatchAd): string {
		if (!other) {
			return null;
		}

		const updates: any[] = [];

		if (this._watchStartTime !== other._watchStartTime) {
			updates.push(E_WatchAd.WATCH_START_TIME, this._watchStartTime);
		}
		if (this._lastWatchEndTime !== other._lastWatchEndTime) {
			updates.push(E_WatchAd.LAST_WATCH_END_TIME, this._lastWatchEndTime);
		}

		const jsonv = updates.length === 0 ? null : JSON.stringify(updates);
		return jsonv;
	}


	/**全部更新*/
	protected dispatchTotalUpdate(): void {
		IAsync.dispatch(WatchAd.TOTAL_EVENT_ID, this.getV(0));
	}

}
